package com.two.screen.water.balance.utils

import java.math.BigDecimal
import java.math.RoundingMode

/**
 * 加、减、乘、除 高精度计算工具类
 * @author lyl 20190191
 * */
object UtilsBigDecimal {

    // 需要精确至小数点后几位
    const val DECIMAL_POINT_NUMBER2: Int = 2
    const val DECIMAL_POINT_NUMBER1: Int = 1

    // 加法运算
    @JvmStatic
    fun add(d1: BigDecimal, d2: Double): BigDecimal = d1.add(BigDecimal(d2)).setScale(DECIMAL_POINT_NUMBER2, BigDecimal.ROUND_HALF_UP)

    // 减法运算
    @JvmStatic
    fun sub(d1: Double, d2: Double): BigDecimal = BigDecimal(d1).subtract(BigDecimal(d2)).setScale(DECIMAL_POINT_NUMBER2, BigDecimal.ROUND_HALF_UP)

    // 乘法运算
    @JvmStatic
    fun mul(d1: Double, d2: Double, decimalPoint: Int = DECIMAL_POINT_NUMBER2): BigDecimal =
        BigDecimal(d1).multiply(BigDecimal(d2)).setScale(decimalPoint, BigDecimal.ROUND_HALF_UP)

    // 除法运算
    @JvmStatic
    fun div(d1: Double, d2: Double): BigDecimal = BigDecimal(d1).divide(BigDecimal(d2)).setScale(DECIMAL_POINT_NUMBER2, BigDecimal.ROUND_HALF_UP)

    @JvmStatic
    fun String.toDoubleWithTwoDecimalPlaces(point: Int = DECIMAL_POINT_NUMBER2): Double {
        return BigDecimal(this).setScale(point, RoundingMode.HALF_EVEN).toDouble()
    }
}
